//
//  EUExLoadingView.m
//  EUExLoadingView
//
//  Created by zhijian du on 15/2/6.
//  Copyright (c) 2015年 zhijian du. All rights reserved.
//

#import "EUExLoadingView.h"
#import "EUtility.h"
@interface EUExLoadingView () <MONActivityIndicatorViewDelegate>
@property (retain,nonatomic)  UIPageControl *page;
@property (retain,nonatomic)  MONActivityIndicatorView *indicatorView;

@end


@implementation EUExLoadingView
{
    NSTimer *timer;
}
-(id)initWithBrwView:(EBrowserView *)eInBrwView{
    self = [super initWithBrwView:eInBrwView];
    if (self) {
        
    }
    return self;
}
-(void)open:(NSMutableArray *)inArguments{
    NSInteger x = 0;
    NSInteger y = 0;
    NSInteger num = 0;
    if (inArguments.count > 0 ) {
        x = [[inArguments objectAtIndex:0] integerValue];
        y = [[inArguments objectAtIndex:1] integerValue];
        if (inArguments.count == 3) {
            num = [[inArguments objectAtIndex:2] integerValue];
        }
    }
    if (num == 0) {
        MONActivityIndicatorView *indicatorView = [[MONActivityIndicatorView alloc] init];
        self.indicatorView = indicatorView;
        [indicatorView release];
        self.indicatorView.delegate = self;
        self.indicatorView.numberOfCircles = 5;
        self.indicatorView.radius = 20;
        self.indicatorView.internalSpacing = 3;
//        self.indicatorView.center = CGPointMake(300, 500);
        [self.indicatorView startAnimating];
        [EUtility brwView:meBrwView addSubview:self.indicatorView];
        CGRect rect = self.indicatorView.frame;
        self.indicatorView.frame = CGRectMake(x, y, rect.size.width, rect.size.height);
//        self.indicatorView.backgroundColor = [UIColor yellowColor];
        [self.indicatorView startAnimating];
//        [NSTimer scheduledTimerWithTimeInterval:7 target:self.indicatorView selector:@selector(stopAnimating) userInfo:nil repeats:NO];
//        [NSTimer scheduledTimerWithTimeInterval:1 target:self.indicatorView selector:@selector(startAnimating) userInfo:nil repeats:NO];
    }else{
        UIPageControl *page=[[UIPageControl alloc]initWithFrame:CGRectMake(x, y , 100, 20)];
        self.page = page;
        [page release];
        self.page.backgroundColor=[UIColor clearColor];
        self.page.numberOfPages=num;
        timer = [NSTimer scheduledTimerWithTimeInterval:0.2f target:self selector:@selector(pageNumberChanged) userInfo:nil repeats:YES];
//        NSTimer *timer=[NSTimer timerWithTimeInterval:0.2f target:self selector:@selector(pageNumberChanged) userInfo:nil repeats:YES];
//        [[NSRunLoop mainRunLoop]addTimer:timer forMode:NSDefaultRunLoopMode];
        [EUtility brwView:meBrwView addSubview:self.page];
    }
}

-(void)pageNumberChanged
{
    static int i=0;
    i++;
    i=i>self.page.numberOfPages?0:i;
    self.page.currentPage=i;
}
#pragma mark -
#pragma mark - MONActivityIndicatorViewDelegate Methods
- (UIColor *)activityIndicatorView:(MONActivityIndicatorView *)activityIndicatorView
      circleBackgroundColorAtIndex:(NSUInteger)index {
    CGFloat red   = (arc4random() % 256)/255.0;
    CGFloat green = (arc4random() % 256)/255.0;
    CGFloat blue  = (arc4random() % 256)/255.0;
    CGFloat alpha = 1.0f;
    return [UIColor colorWithRed:red green:green blue:blue alpha:alpha];
}
#pragma mark -

-(void)close:(NSMutableArray *)inArguments{
    if(timer){//先移除定时器
        [timer invalidate];
    }

    if (self.page) {
        [_page removeFromSuperview];
        self.page = nil;
    }
    if (self.indicatorView) {
        [_indicatorView removeFromSuperview];
        self.indicatorView = nil;
    }
}
-(void)clean{
    if(timer){//先移除定时器
        [timer invalidate];
    }
    if (self.page) {
        [_page removeFromSuperview];
        self.page = nil;
    }
    if (self.indicatorView) {
        [_indicatorView removeFromSuperview];
        self.indicatorView = nil;
    }

}
-(void)dealloc{
    if(timer){//先移除定时器
        [timer invalidate];
    }
    if (self.page) {
        [_page removeFromSuperview];
        self.page = nil;
    }
    if (self.indicatorView) {
        [_indicatorView removeFromSuperview];
        self.indicatorView = nil;
    }

    [super dealloc];
}

@end
